package org.openit.portail.web;

import javax.validation.Valid;

import org.openit.portail.model.Client;
import org.openit.portail.model.Panier;
import org.openit.portail.service.ServiceService;
import org.openit.portail.service.UtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(MainController.URL)
public class MainController {
	
	static final String URL = "/";
	
	@Autowired
	private ServiceService serviceService;
	@Autowired
	private UtilService utilService;
	
	@ModelAttribute("panier")
	public Panier getPanier(){
		return utilService.getPanier();
	}
	@RequestMapping(method = RequestMethod.GET)
	public String getHomePage(ModelMap model) {
		return "acceuil";
	}
	@RequestMapping(method = RequestMethod.GET,value="login")
	public String getLoginPage(@RequestParam(value = "error", required = false) boolean error,
			ModelMap model) {
		if (error == true) {
			model.put("error",
					"You have entered an invalid username or password!");
		} else {
			model.put("error", "");
		}
		return "authentification";
	}
	@RequestMapping(method = RequestMethod.GET,value="nous")
	public String getNousPage(ModelMap model) {
		return "nous";
	}
	@RequestMapping(method = RequestMethod.GET,value="produits")
	public String getProduitsPage(ModelMap model) {
		model.put("produits", serviceService.getServices());
		return "produits";
	}
	@RequestMapping(method = RequestMethod.GET,value="contact")
	public String getContactPage(ModelMap model) {
		return "contact";
	}
		@RequestMapping(method = RequestMethod.GET,value="ousomme")
		public String getOusommePage(ModelMap model) {
			return "ousomme";
	}
	@RequestMapping(method = RequestMethod.POST,value="ginscription")
	public String getContactPage(String email,ModelMap model) {
		Client client=new Client();
		client.setEmail(email);
		model.put("newclient", client);
		return "inscription";
	}
	@RequestMapping(method = RequestMethod.POST,value="inscription")
	public String getContactPage(@Valid Client client,ModelMap model) {
		System.out.println(client.getAdresse());
		return "acceuil";
	}
	@RequestMapping(method = RequestMethod.POST,value="contact")
	public String sendContact(@RequestParam String nom,@RequestParam String from,@RequestParam String objet,@RequestParam String msg,@RequestParam(required=false) String scopie,ModelMap model) {
		if(scopie==null){
			utilService.sendContactMail(nom, from, objet, msg, 0);
		}else{
			utilService.sendContactMail(nom, from, objet, msg, 1);
		}
		model.put("notif", "Merci pour votre e-mail");
		return "contact";
	}
	@RequestMapping(method = RequestMethod.GET,value="messervice")
	public String mesService(ModelMap model){
		model.put("servicelist", serviceService.getServiceForClient());
		return "messervices";
	}
}
